<?php

namespace App\Console\Commands;

use App\Imports\MultipleImport;
use App\Models\JobType;
use App\Models\ModelNumber;
use App\Models\ModelNumberSpecification;
use App\Models\Product;
use App\Models\ProductCenter;
use App\Models\ProductJobType;
use App\Models\ProductModelNumber;
use App\Models\Specification;
use Illuminate\Console\Command;
use Maatwebsite\Excel\Facades\Excel;

class AddProduct extends Command
{
    /**
     * 批量录入产品，型号，规格
     * php artisan command:addproduct
     * @var string
     */
    protected $signature = 'command:addproduct';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     *
     * @return int
     */


    public function handle()
    {
        $file = public_path('excel').'/产品.xlsx';//获取文件路径
        $import = new MultipleImport(3);
        Excel::import($import, $file);
        $results = $import->sheet[0]->data->toArray();
        //$results2 = $import->sheet[1]->data->toArray();
        //$results3 = $import->sheet[2]->data->toArray();
        //dd($results,$results2,$results3);
        unset($results[0]);
        unset($results[1]);
        foreach($results as $k=>$v){
            $this->info('导入：产品名称='.$v[1].";型号=".$v[2].";规格=".$v[4]);
            $product_obj = Product::updateOrCreate(['product_name'=>$v[1]],['product_name'=>$v[1]]);
            $model_number_obj = ModelNumber::updateOrCreate(['model_number'=>$v[2]],['model_number'=>$v[2]]);
            ProductModelNumber::updateOrCreate(
                ['model_number_id'=>$model_number_obj->id,'product_id'=>$product_obj->id],
                ['model_number_id'=>$model_number_obj->id,'product_id'=>$product_obj->id]
            );
            if(!empty($v[4])){
                if(strstr($v[4],'/')){
                    $arr = explode('/',$v[4]);
                    foreach($arr as $kk=>$vv){
                        $specification_obj = Specification::updateOrCreate(['specification'=>$vv],['specification'=>$vv]);
                        ModelNumberSpecification::updateOrCreate(
                            ['model_number_id'=>$model_number_obj->id,'specification_id'=>$specification_obj->id],
                            [
                                'model_number_id'=>$model_number_obj->id,
                                'specification_id'=>$specification_obj->id,
                                'product_id'=>$product_obj->id
                            ]
                        );
                    }
                }else{
                    $specification_obj = Specification::updateOrCreate(['specification'=>$v[4]],['specification'=>$v[4]]);
                    ModelNumberSpecification::updateOrCreate(
                        ['model_number_id'=>$model_number_obj->id,'specification_id'=>$specification_obj->id],
                        [
                            'model_number_id'=>$model_number_obj->id,
                            'specification_id'=>$specification_obj->id,
                            'product_id'=>$product_obj->id
                        ]
                    );
                }

            }



        }
    }
}
